package com.yzcrm.erp.crm.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yzcrm.common.annotation.Permissions;
import com.yzcrm.common.config.paragetter.BasePageRequest;
import com.yzcrm.erp.admin.service.AdminSceneService;
import com.yzcrm.erp.crm.common.CrmEnum;
import com.yzcrm.erp.crm.service.CrmRecordService;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.paragetter.Para;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.yzcrm.utils.AuthUtil;
import com.yzcrm.utils.R;

import java.util.List;


public class CrmRecordController extends Controller {

    @Inject
    private CrmRecordService crmRecordService;
    
    @Inject
    private AdminSceneService adminSceneService;
    /**
     * @author hmb
     * 查询操作记录列表
     */
    public void queryRecordList(){
        String actionId = getPara("actionId");
        String types = getPara("types");
        boolean auth;
        if("2".equals(types)){
            auth = AuthUtil.isPoolAuth(Integer.valueOf(actionId));
        }else {
            auth = AuthUtil.isCrmAuth(AuthUtil.getCrmTablePara(CrmEnum.parse(Integer.valueOf(types))), Integer.valueOf(actionId));
        }
        if(auth){renderJson(R.noAuth()); return; }
        renderJson(crmRecordService.queryRecordList(actionId,types));
    }

    /**
     * @author wyq
     * 删除跟进记录
     */
    public void deleteFollowRecord(@Para("recordId")Integer recordId){
        renderJson(crmRecordService.deleteFollowRecord(recordId));
    }

    /**
     * @author wyq
     * 跟进记录类型设置
     */
    public void queryRecordOptions(){
        renderJson(crmRecordService.queryRecordOptions());
    }

    /**
     * @author wyq
     * 设置跟进记录类型
     */
    public void setRecordOptions(){
        JSONObject jsonObject = JSONObject.parseObject(getRawData());
        JSONArray jsonArray = JSONArray.parseArray(jsonObject.getString("value"));
        List<String> list = jsonArray.toJavaList(String.class);
        renderJson(crmRecordService.setRecordOptions(list));
    }
    
    /**
     * @author lg
     * 	跟进记录内容设置
     */
    public void queryRecordContentTemplate(){
        renderJson(crmRecordService.queryRecordContentTemplate());
    }

    /**
     * @author lg
     * 	设置跟进记录内容
     */
    public void setRecordContentTemplate(){
        JSONObject jsonObject = JSONObject.parseObject(getRawData());
        String value = jsonObject.getString("value");
        renderJson(crmRecordService.setRecordContentTemplate(value));
    }
    
    /**
     * @author lg
     * 	查看列表页
     */
    public void queryFollowRecords(BasePageRequest basePageRequest) {
        JSONObject jsonObject = basePageRequest.getJsonObject();
        basePageRequest.setJsonObject(jsonObject);
        renderJson(crmRecordService.queryFollowRecords(basePageRequest));
    }
    
    /**
     * @author lg
     * 	查看跟进记录详情
     */
    public void queryFollowRecordById(@Para("recordId")Integer recordId) {
        renderJson(crmRecordService.queryFollowRecordById(recordId));
    }
    
    /**
     * @author lg
     * 	查询跟进记录详情
     */
    public void queryFollowRecordContentFields(){
    	Kv kv = new Kv();
    	List<Record> records = Db.find("select * from yzcrm_admin_config where modular_type = 'crmFollowRecordContent';");
    	kv.set("list", records);
    	renderJson(R.ok().put("data",kv));
    }
}
